package com.newsmanagement.controller.backstage;

import com.baomidou.mybatisplus.plugins.Page;
import com.newsmanagement.config.RedisClient;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IEumService;
import com.newsmanagement.service.INewsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author xbr
 * 2017.12.04 11:24
 * 后台系统 新闻分类
 */
@Controller
@RequestMapping("newCategoryBack")
public class NewsCategoryBackstageController {

    @Autowired
    private INewsCategoryService newsCategoryService;


    @Autowired
    private RedisClient redisClinet;

    /**
     * 设置key和value到redis
     * @param key
     * @param value
     * @return
     * @throws Exception
     * http://localhost:8080/newCategoryBack/setTest?key=name&value=zhi
     */
    @RequestMapping("/setTest")
    @ResponseBody
    public String set(
            @RequestParam(value = "key") String key ,
            @RequestParam(value = "value") String value

    ) throws Exception{
        redisClinet.set(key, value, 10);
        return "success";
    }

    /**
     * 根据key获取value
     * @param key
     * @param value
     * @return
     * @throws Exception
     * http://localhost:8080/newCategoryBack/getTest?key=name
     */
    @RequestMapping("/getTest")
    @ResponseBody
    public String get(
            @RequestParam(value = "key") String key
    ) throws Exception {
        return redisClinet.get(key);
    }

    /**
     * 分页查询新闻分类列表
     * @param jumpPage 跳转页面
     * @param showSize 每页显示的数据条数
     * @return ReturnedMessages
     * http://localhost:8080/newCategoryBack/list?jumpPage=1&showSize=5
     */
    @RequestMapping("list")
    @ResponseBody
    public ReturnedMessages catatoryList(
            @RequestParam(value = "jumpPage") Integer jumpPage ,
            @RequestParam(value = "showSize") Integer showSize
    ){

        return newsCategoryService.categoryList(jumpPage ,showSize);
    }

    /**
     * 添加新闻分类
     * 根据分类名称添加分类
     * 排序：默认最后，等于当前categoryId（默认自增的主键）
     * @param categoryName 分类名称
     * @return ReturnedMessages
     * 测试：http://localhost:8080/newCategoryBack/add?categoryName=书籍
     */
    @RequestMapping("add")
    @ResponseBody
    public ReturnedMessages addCatatory(@RequestParam(value = "categoryName") String categoryName){

        return newsCategoryService.addCategory(categoryName);
    }

    /**
     * 根据分类id修改对应的新闻分类信息
     * @param categoryId 分类id
     * @param categoryName 分类名称
     * @return
     * http://localhost:8080/newCategoryBack/update?categoryName=书籍1111111&categoryId=17
     */
    @RequestMapping("update")
    @ResponseBody
    public ReturnedMessages updateCatatory(
            @RequestParam(value = "categoryId") Integer categoryId ,
            @RequestParam(value = "categoryName") String categoryName
    ){

        return newsCategoryService.updateCategory(categoryId ,categoryName);
    }

    /**
     * 根据分类id删除对应的新闻分类信息
     * @param categoryId 分类id
     * @return
     * http://localhost:8080/newCategoryBack/delete?categoryId=17
     */
    @RequestMapping("delete")
    @ResponseBody
    public ReturnedMessages deleteCatatory(
            @RequestParam(value = "categoryId") Integer categoryId
    ){

        return newsCategoryService.deleteCategory(categoryId);
    }

    /**
     * 新闻分类排序手动调整
     * @param categoryId 分类id
     * @param operating 操作编码：0，跳到最后方;1,前移一级；2，后移一级；3，跳到最前方；默认为0
     * @return ReturnedMessages
     * http://localhost:8080/newCategoryBack/move?categoryId=17&operating=3
     * http://localhost:8080/newCategoryBack/move?categoryId=10&operating=1
     */
    @RequestMapping("move")
    @ResponseBody
    public ReturnedMessages moveCatatoryOrder(
            @RequestParam(value = "categoryId") Integer categoryId ,
            @RequestParam(value = "operating") Integer operating
    ){

        return newsCategoryService.moveCatatoryOrder(categoryId ,operating);
    }

}
